package com.example.aspect;
import com.example.page.IPageQuery;
import com.github.pagehelper.PageHelper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/*
@description:
@ClassName PageQueryAspect
@author chen
@create 2025-07-15 14:14
@Version 1.0
*/
@Aspect
@Order
@Component
public class PageQueryAspect
{

    /*
     * @param pjp
     * @==============History===============<br/>;
     * @Description // 拦截mapper中的所有方法
     * @Date 2025/7/15
     * @Author < a href="chencheng@prmt.net">chen</ a>;
     */
    @Around("execution(* com.example..*Mapper.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable
    {
        boolean pageFlag = false;
        try
        {
            // 遍历参数，参数中如果有 IPageQuery 类型的，则从 IPageQuery 取出分页信息，则使用 PageHelper 开启分页
            Object[] args = pjp.getArgs();
            for (Object arg : args)
            {
                if (arg instanceof IPageQuery)
                {
                    IPageQuery pageQuery = (IPageQuery) arg;
                    PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize(), pageQuery.count());
                    pageFlag = true;
                    break;
                }
            }
        }
        finally
        {
            if (pageFlag)PageHelper.clearPage();// 清理分页信息
        }
        return null;
    }
}
